#!/usr/local/bin/php
<?php
	require_once("connection.php");
	require_once("functions.php");
?>
<?php
	if(isset($_POST))
	{
		if(isset($_POST['action']) && !empty($_POST['action'])){
			$action = $_POST['action'];
			if($action === "addUser")
			{
				$username = ""; $password = ""; $result = "Empty";
				if(isset($_POST['username']) && !empty($_POST['username'])){
					$username = mysql_prep($_POST['username']);
				}
				if(isset($_POST['password']) && !empty($_POST['password'])){
					$password = mysql_prep($_POST['password']);
				}
				if(!empty($username) && !empty($password)){
					$result = insertUser($username,$password);
				}
				$response = "{\"result\":\"{$result}\"}";
				echo $response;
			}
			else if($action === "authenticate")
			{
				$username = ""; $password = ""; $result = "Empty";
				if(isset($_POST['username']) && !empty($_POST['username'])){
					$username = mysql_prep($_POST['username']);
				}
				if(isset($_POST['password']) && !empty($_POST['password'])){
					$password = mysql_prep($_POST['password']);
				}
				if(!empty($username) && !empty($password)){
					$result = authenticateUser($username,$password);
				}
				$response = "{\"result\":\"{$result}\"}";
				echo $response;
			}
			else if($action === "follow")
			{
				$me = ""; $friend = ""; $result = "FollowEmpty";
				if(isset($_POST['me']) && !empty($_POST['me'])){
					$me = mysql_prep($_POST['me']);
				}
				if(isset($_POST['friend']) && !empty($_POST['friend'])){
					$friend = mysql_prep($_POST['friend']);
				}
				if(!empty($me) && !empty($friend)){
					$result = addFriend($me,$friend);
				}
				$response = "{\"result\":\"{$result}\"}";
				echo $response;
			}
			else if($action === "remove")
			{
				$me = ""; $friend = "";
				if(isset($_POST['me']) && !empty($_POST['me']))
				{
					$me = mysql_prep($_POST['me']);
				}
				if(isset($_POST['friend']) && !empty($_POST['friend'])){
					$friend = mysql_prep($_POST['friend']);
				}
				if(!empty($me) && !empty($friend)){
					$result = removeFriend($me,$friend);
				}
				$response = "{\"result\":\"{$result}\"}";
				echo $response;
			}
			else if($action === "getFriends")
			{
				$me = "";
				if(isset($_POST['me']) && !empty($_POST['me']))
				{
					$me = mysql_prep($_POST['me']);
				}
				if(!empty($me)){
					$result = getFriends($me);
				}
				$response = "{\"result\":{$result}}";
				echo $response;
			}
			else if($action === "getMyDishes")
			{
				if(isset($_POST['me']) && !empty($_POST['me'])){
					$me = mysql_prep($_POST['me']);
				}
				$result = getMySentDishes($me);
				$response = "{\"result\":{$result}}";
				echo $response;
			}
			else if($action === "getMyFollowsDishes")
			{
				if(isset($_POST['me']) && !empty($_POST['me'])){
					$me = mysql_prep($_POST['me']);
				}
				$result = getMyReceivedDishes($me);
				$response = "{\"result\":{$result}}";
				echo $response;
			}
		}
		else
		{
			$result = "DishEmpty"; $dishData = ""; $fileName = "";
			if(isset($_POST['Dish']) && !empty($_POST['Dish'])){
				$dishData = $_POST['Dish'];
				if(isset($_FILES['Image']) && !empty($_FILES['Image'])){
					$fileName = time().$_FILES['Image']['name'];
					move_uploaded_file($_FILES['Image']['tmp_name'],"images/".$fileName);
				}
				$result = addDish($dishData,$fileName);
			}
			else if(isset($_POST['Store']) && !empty($_POST['Store'])){
				$dishData = $_POST['Store'];
				if(isset($_FILES['Image']) && !empty($_FILES['Image'])){
					$fileName = time().$_FILES['Image']['name'];
					move_uploaded_file($_FILES['Image']['tmp_name'],"images/".$fileName);
				}
				$result = storeDish($dishData,$fileName);
			}
			$response = "{\"result\":\"{$result}\"}";
			echo $response;
		}
	}
?>
<?php
	require_once("footer.php");
?>